/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InfoTree.java
 *
 * Created on Mar 13, 2010, 12:04:48 PM
 */
package chat.client;

import javax.swing.JTree;
import javax.swing.tree.*;
import java.util.*;
import java.lang.*;
import java.io.*;
import javax.swing.*;
import java.awt.HeadlessException;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Iterator;

/**
 * The application's info Tree for channel and users.
 * @author seb
 */
public class InfoTree extends javax.swing.JPanel {

    private DefaultMutableTreeNode listClient;
    private DefaultMutableTreeNode listChannel;
    private DefaultMutableTreeNode root;
    private HashSet<String> vectClient = new HashSet();
    private HashSet<String> vectChannel = new HashSet();
    private ChatView client;

    /** Creates new form InfoTree */
    public InfoTree(ChatView client, HashSet<String> vectClient) {
        initComponents();
        this.client = client;

        root = new DefaultMutableTreeNode("Name");
        DefaultMutableTreeNode parent;

        parent = new DefaultMutableTreeNode("Client");
        root.add(parent);
        listClient = parent;

        parent = new DefaultMutableTreeNode("Channel");
        root.add(parent);
        listChannel = parent;

        infoTree.setModel(new DefaultTreeModel(root));

        infoTree.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }

            public void mousePressed(MouseEvent e) {
            }

            public void mouseReleased(MouseEvent e) {
                /* recup´ration du chemin associ´ au noeud s´lectionn´ */
                TreePath selPath = infoTree.getPathForLocation(e.getX(), e.getY());
                if (selPath != null && selPath.getPathCount() > 2) {
                    System.out.println(selPath.getPathComponent(selPath.getPathCount() - 1));
                    String st = selPath.getPathComponent(selPath.getPathCount() - 1).toString();
                    newChat(st); // sinon, ça marche pas !
                }
            }
        });
    }

    /*
     * creation du nouvel onglet pour la conversation
     */
    private void newChat(String st) {
        client.newChat(st, true);
    }

    /*
     * nouvelle connexion, on a la list entiere a cree
     */
    public void notifyList(HashSet<String> vectClient, HashSet<String> vectChannel) {
        this.vectClient = vectClient;
        this.vectChannel = vectChannel;
        listClient.removeAllChildren();
        listChannel.removeAllChildren();
        for (String idClient : vectClient) {
            listClient.add(new DefaultMutableTreeNode(idClient));
        }
        for (String idChannel : vectChannel) {
            listChannel.add(new DefaultMutableTreeNode(idChannel));
        }
        infoTree.setModel(new DefaultTreeModel(root));
//        infoTree.expandRow(infoTree.getRowCount()+1);
//        infoTree.setExpandsSelectedPaths(true);
//        infoTree.expandPath(root);
        //       infoTree.expandPath(new TreePath(root.getPath()));
        this.extandTree();
    }

    public void newClient(String idclient) {
        vectClient.add(idclient);
        listClient.removeAllChildren();
        for (String idClient : vectClient) {
            listClient.add(new DefaultMutableTreeNode(idClient));
        }
        infoTree.setModel(new DefaultTreeModel(root));
        this.extandTree();
    }

    public void removeClient(String idclient) {
        vectClient.remove(idclient);
        listClient.removeAllChildren();
        for (String idClient : vectClient) {
            listClient.add(new DefaultMutableTreeNode(idClient));
        }
        infoTree.setModel(new DefaultTreeModel(root));
        this.extandTree();
    }

    public void newChannel(String channel) {
        vectChannel.add(channel);
        listChannel.removeAllChildren();
        for (String channels : vectChannel) {
            listChannel.add(new DefaultMutableTreeNode(channels));
        }
        infoTree.setModel(new DefaultTreeModel(root));
        this.extandTree();
    }

    public void removeChannel(String channel) {
        vectChannel.remove(channel);
        listChannel.removeAllChildren();
        for (String channels : vectChannel) {
            listChannel.add(new DefaultMutableTreeNode(channels));
        }
        infoTree.setModel(new DefaultTreeModel(root));
        this.extandTree();
    }

    private void extandTree() {
        int i = 0;
        while (i < infoTree.getRowCount()) {
            infoTree.expandRow(i);
            i++;
        }
    }

    /*
     * dans la racine de l'arbre, on affiche le nom du connecter (soi-meme)
     */
    public void setNewName(String name) {
        root = new DefaultMutableTreeNode(name);
        root.add(listClient);
        root.add(listChannel);
        infoTree.setModel(new DefaultTreeModel(root));
    }

    public boolean channelExist(String channel) {
        boolean b = false;
        for (String channels : vectChannel) {
            if (channel.equalsIgnoreCase(channels)) {
                b = true;
            }
        }
        return b;
    }

    public ChatView getClient() {
        return client;
    }

    public void setClient(ChatView client) {
        this.client = client;
    }

    public HashSet<String> getVectClient() {
        return vectClient;
    }

    public void setVectClient(HashSet<String> vectClient) {
        this.vectClient = vectClient;
    }

    public HashSet<String> getVectChannel() {
        return vectClient;
    }

    public void setVectChannel(HashSet<String> vectChannel) {
        this.vectChannel = vectChannel;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        infoTree = new javax.swing.JTree();

        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setOpaque(false);
        setLayout(new java.awt.BorderLayout());

        jScrollPane1.setName("jScrollPane1"); // NOI18N
        jScrollPane1.setOpaque(false);

        infoTree.setName("infoTree"); // NOI18N
        jScrollPane1.setViewportView(infoTree);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTree infoTree;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
